#!/bin/bash

set -e

# 清除环境
#docker-compose down -v
#rm -rf /tmp/ssl /data/cert /etc/docker/certs.d
# 关闭防火墙
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config


# 生成根证书
mkdir /tmp/ssl && cd /tmp/ssl
openssl genrsa -out ca.key 4096
openssl req -x509 -new -nodes -sha512 -days 36500 \
    -subj "/C=CN/ST=HuBei/L=Wuhan/O=example/OU=Personal/CN=Harbor-ca" \
    -key ca.key \
    -out ca.crt

# 生成服务器端证书
domain=$(hostname -I | awk '{print $1}')
openssl genrsa -out server.key 4096
openssl req -sha512 -new \
    -subj "/C=CN/ST=HuBei/L=Wuhan/O=example/OU=Personal/CN=$domain" \
    -key server.key \
    -out server.csr

echo subjectAltName = IP:$domain > extfile.cnf
openssl x509 -req -sha512 -days 36500 \
    -extfile extfile.cnf \
    -CA ca.crt -CAkey ca.key -CAcreateserial \
    -in server.csr \
    -out server.crt

# 配置harbor证书
mkdir -p /data/cert
cp server.crt server.key /data/cert

# 安装docker
bash <(curl -sSL https://gitee.com/yx571304/my_oschina/raw/master/docker/install.sh)

# 配置docker服务器证书
#openssl x509 -inform PEM -in server.crt -out server.cert
mkdir -p /etc/docker/certs.d/$domain
#cp server.cert /etc/docker/certs.d/$domain
#cp server.key /etc/docker/certs.d/$domain
cp ca.crt /etc/docker/certs.d/$domain
systemctl restart docker
systemctl restart docker

# 安装harbor
cd
FileUrl='https://docker-1252143340.cos.ap-chengdu.myqcloud.com/harbor-offline-installer-v1.7.0.tgz'
curl -SLO $FileUrl --progress
tar xvf harbor-offline-installer-v1.7.0.tgz
cd harbor
sed -i \
  -e "s/reg.mydomain.com/`hostname -I | awk '{print $1}'`/" \
  -e "s/ui_url_protocol = .*/ui_url_protocol = https/" \
  -e "s/Harbor12345/admin/" \
  harbor.cfg
./prepare
./install.sh
